//
//  UUOrderAssignVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 23/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUOrderAssignVC.h"
#import "UUHybridManager+CharterSupplier.h"
#import "UUOrderManager.h"
#import "UUFleetManager.h"
#import "UUCarManager.h"
#import "UUDriverManager.h"
#import <UUFoundation/UIBarButtonItem+CustomButton.h>

@interface UUOrderAssignVC ()

@property (nonatomic, strong) UUOrderModel *order;

@end

@implementation UUOrderAssignVC

- (void)viewDidLoad {
    self.editable = YES;
    
    [super viewDidLoad];
    
    DKAssert(self.orderId != nil);
    
    self.order = [GetOrderManager() fetchOrderWithOrderId:self.orderId];

    [self.context.userInfo setObject:self.order.carCategoryId forKey:@"greaterThanOrEqualToCarCategory"];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"提交"
                                                                              target:self
                                                                            selector:@selector(submit)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)fullName {
    return @"分配司机与车辆";
}

- (NSURLRequest *)editRequest {
    return GetHybridManager().orderAssignURLRequest;
}

- (NSDictionary *)previewFields {
    if (self.order.status == UUOrderStatusWaitingForAssign) {
        return @{
                 @"count" : @(self.order.carNum)
                 };
    } else if (self.order.status == UUOrderStatusWaitingForService) {
        NSMutableArray *itemDicts = [NSMutableArray arrayWithCapacity:self.order.dispatch.count];
        for (UUOrderDispatchItemModel *itemModel in self.order.dispatch) {
            UUFleetModel *fleet = [GetFleetManager() fetchFleetWithFleetId:itemModel.fleetId];
            UUCarModel *car = [GetCarManager() fetchCarWithCarId:itemModel.carId];
            UUDriverModel *driver = [GetDriverManager() fetchDriverWithDriverId:itemModel.driverId];
            
            NSMutableDictionary *itemDict = [NSMutableDictionary dictionaryWithCapacity:6];
            SAFE_SET_DICT(itemDict, @"motorcade_id", fleet.fleetId);
            SAFE_SET_DICT(itemDict, @"bus_id", car.carId);
            SAFE_SET_DICT(itemDict, @"driver_id", driver.driverId);
            SAFE_SET_DICT(itemDict, @"motorcade_name", fleet.name);
            SAFE_SET_DICT(itemDict, @"bus_name", car.carNum);
            SAFE_SET_DICT(itemDict, @"driver_name", driver.name);
            
            [itemDicts addObject:itemDict];
        }
        return @{
                 @"count" : @(self.order.carNum),
                 @"data" : itemDicts
                 };
    } else {
        return nil;
    }
}

- (void)addOrUpdate:(NSDictionary *)data {
    NSArray *rawData = data[@"data"];
    NSMutableArray<UUOrderDispatchItemModel *> *items = [NSMutableArray arrayWithCapacity:rawData.count];
    for (NSDictionary *dict in rawData) {
        UUOrderDispatchItemModel *item = [[UUOrderDispatchItemModel alloc] initWithData:dict];
        item.driverMobile = [GetDriverManager() fetchDriverWithDriverId:item.driverId].mobile;
        [items addObject:item];
    }
    
    ShowIndicator();
    WEAK_SELF();
    [GetOrderManager() asyncAssignOrderWithOrderId:self.orderId
                                        dispatches:items
                                     completeBlock:^(NSError *error) {
                                         if (error == nil) {
                                             HideIndicatorInView(weakSelf.view);
                                             [weakSelf.navigationController popViewControllerAnimated:YES];
                                         } else {
                                             ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                         }
                                     }];
}

@end
